package com.zwz.service.impl;

import com.zwz.dto.ExecutedPersonDTO;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.entity.ExecutedPerson;
import com.zwz.entity.RiskInfoMain;
import com.zwz.mapper.ExecutedPersonMapper;
import com.zwz.mapper.RiskInfoMainMapper;
import com.zwz.service.ExecutedPersonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Slf4j
@Service
@Transactional
public class ExecutedPersonServiceImpl implements ExecutedPersonService {

    @Autowired
    private RiskInfoMainMapper mainMapper;

    @Autowired
    private ExecutedPersonMapper executionMapper;

    @Override
    public ExecutedPersonDTO createWithMain(ExecutedPerson entity, String enterpriseId) {
        try {
            // 创建主表记录
            EnterpriseInfo enterpriseInfo = new EnterpriseInfo();
            enterpriseInfo.setId(Long.valueOf(enterpriseId));

            // 设置子表关联
            ExecutedPerson execution = new ExecutedPerson();
            execution.setCaseNumber(entity.getCaseNumber());
            execution.setExecutionAmount(entity.getExecutionAmount() != null ? 
                entity.getExecutionAmount() : new BigDecimal(1.00));
            execution.setHasExecution(entity.getHasExecution());
            execution.setHistoricalFilingDate(entity.getHistoricalFilingDate());
            execution.setFilingDate(entity.getFilingDate());
            execution.setEnterpriseId(Integer.valueOf(enterpriseId));

            // 插入子表
            executionMapper.insert(execution);
            log.info("成功创建被执行人记录: {}", enterpriseId);

            return new ExecutedPersonDTO(enterpriseInfo, execution);
        } catch (Exception e) {
            log.error("创建被执行人记录失败: {}", e.getMessage(), e);
            throw new RuntimeException("创建被执行人记录失败", e);
        }
    }

    @Override
    public void updateExecution(ExecutedPerson execution) {
        try {
            executionMapper.update(execution);
            log.info("成功更新被执行人记录: {}", execution.getEnterpriseId());
        } catch (Exception e) {
            log.error("更新被执行人记录失败: {}", e.getMessage(), e);
            throw new RuntimeException("更新被执行人记录失败", e);
        }
    }

    @Override
    public List<ExecutedPerson> getActiveExecutions() {
        try {
            return executionMapper.selectActiveExecutions();
        } catch (Exception e) {
            log.error("获取活跃被执行人记录失败: {}", e.getMessage(), e);
            throw new RuntimeException("获取活跃被执行人记录失败", e);
        }
    }

    private RiskInfoMain createMainRecord(Integer companyId) {
        RiskInfoMain main = new RiskInfoMain();
        main.setCompanyId(companyId);
        mainMapper.insert(main);
        return main;
    }

    private void validateExecution(ExecutedPerson execution) {
        if (execution.getHasExecution() != null &&
                execution.getHasExecution() &&
                execution.getFilingDate() == null) {
            throw new IllegalArgumentException("被执行人记录必须包含立案日期");
        }

        if (execution.getExecutionAmount() == null ||
                execution.getExecutionAmount().compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("执行标的金额不能为负数");
        }
    }
}